﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using TracNghiemTinHocDaiCuong.Common;
using TracNghiemTinHocDaiCuong.ThucThe;

namespace TracNghiemTinHocDaiCuong.DAO
{
    public class NhatKiDAO
    {
        public static NhatKi DoiTuong(IDataRecord read)
        {
            return new NhatKi
            {
                MaNK = Methods.ConvertNumber(read["MaNK"].ToString()),
                MaND = Methods.ConvertNumber(read["MaND"].ToString()),
                ThaoTac = Methods.ConvertString(read["ThaoTac"].ToString()),
                NgayXayRa = Methods.ConvertDateTime(read["NgayXayRa"].ToString())
            };
        }

        public static IList<NhatKi> LayDanhSach()
        {
            IList<NhatKi> danhSach = new List<NhatKi>();
            string query = string.Format("SELECT * FROM NhatKi");
            IDataReader read = SqlHelper.ExecuteReader(query);
            while(read.Read())
            {
                danhSach.Add(DoiTuong(read));
            }
            return danhSach;
        }

        public static bool Them(NhatKi nhatKi)
        {
            string query = string.Format("INSERT INTO NhatKi(MaND, ThaoTac, NgayXayRa) VALUES(@MaND, @ThaoTac, @NgayXayRa)");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaND", nhatKi.MaND),
                new SqlParameter("@ThaoTac", nhatKi.ThaoTac),
                new SqlParameter("@NgayXayRa", nhatKi.NgayXayRa)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }

        public static bool Sua(NhatKi nhatKi)
        {
            string query = string.Format("UPDATE NhatKi SET MaND=@MaND, ThaoTac=@ThaoTac, NgayXayRa=@NgayXayRa " +
            "WHERE MaNK=@MaNK");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaNK", nhatKi.MaNK),
                new SqlParameter("@MaND", nhatKi.MaND),
                new SqlParameter("@ThaoTac", nhatKi.ThaoTac),
                new SqlParameter("@NgayXayRa", nhatKi.NgayXayRa)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }

        public static bool Xoa(NhatKi nhatKi)
        {
            string query = string.Format("DELETE FROM NhatKi WHERE MaNK=@MaNK");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaNK", nhatKi.MaNK)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }
    }
}